<style>
	.dropdown select{
		padding:2px;
		width:100px;
		border:1px solid #ddd;
	}
	.dropdown label:first-child{
		margin-left:0;
	}
	.dropdown label{
		margin-left:10px;
		margin-bottom:5px;
	}
</style>
<div class="col-md-8">
<div class="main-content" id="main-content">
	<div class="sub-content">
		<div class="top-header products clearfix">
			<div class="pull-left pad">
				<h1>Product List</h1>
			</div>
			<div class="product-list-details">
				<a href="<? echo $this->Url('products')?>/add-product?is_first=1"><input type="button" value="Add product" class="btn"/></a>				
			</div>
		</div>
		<div class="user-profile1 col-md-12">
		<form id="formid" method="post" class="form-horizontal" role="form">
				<? if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=='admin'){?>
				<div class="form-group">
				<label class="control-label col-sm-3">Users:</label>
<div class="col-sm-9">				
				<select id="users" name="users" onchange="changeUserlist(this.value)">
					<option value="">--Select a user--</option>
					<? if(count($this->allusers)!=0){ foreach($this->allusers as $users){?>
						<option value="<? echo $users->user_id;?>"><? echo $users->company_name;?></option>
					<?}}?>
				</select>
</div>
				</div>				
				<?}?>
				<div class="form-group">
				<label class="control-label col-sm-3">Country:</label>
				<div class="col-sm-9">
				<select id="country" name="country" onchange="changeCategory('country')">
					<option value="all">--Select Country--</option>
				</select>
				</div>
				</div>
				<div class="form-group">
				<label class="control-label col-sm-3">Industry:</label>
				<div class="col-sm-9">
				<select id="industry" name="industry" onchange="changeCategory('industry')">
					<option value="">--Select Industry--</option>
					<? if(count($this->industries)>0){foreach($this->industries as $industries){?>
						<option value="<? echo $industries->industry_id;?>"><? echo $industries->industryname;?></option>
					<?}}?>
				</select>
				</div>
				</div>
				<div class="form-group">
				<label class="control-label col-sm-3">Categories:</label>
				<div class="col-sm-9">
				<select id="category" name="category" onchange="changeCategory('category')">
					<option value="">--Select category--</option>					
				</select>
				</div>
				</div>
				
			</div>
		<div class="product_table table-responsive">			
		</div></form>
	</div>
</div> </div>
<script>
function changeUserlist(user_id){
	$.ajax({
		type:	'POST',
		url:   	BASE_PATH+'/products/get-user-regions',
		data:{user_id:user_id},
		success: function(data){
			if(data.output=='success'){
				$("#country").html(data.getallcountries);
				changeCategory('all');
			}else if(data.output=='not success'){
				$("#country").html(data.getallcountries);
				changeCategory('all');
			}
		}
	});
}
function changeIndustry(industry_id){
	if(industry_id!=''){
		var country_id=$("#country").val();
		$.ajax({
			type:	'POST',
			url:   	BASE_PATH+'/products/get-categories',
			data:	{country_id:country_id,industry_id:industry_id,check:'check'},
			success: function(data){
				if(data.output=='success'){
					$("#category").html(data.categories);
				}
			}
		});
	}else if(industry_id=='all'){
		changeCategory('all');
	}else{
		changeCategory('all');
	}
}
function changeCountry(){
	var country_id=$("#country").val();
	$.ajax({
		type:	'POST',
		url:   	BASE_PATH+'/products/get-industries',
		data:	{country_id:country_id},
		success: function(data){
			if(data.output=='success'){
				$("#industry").html(data.industries);
			}
		}
	});
}
$(function(){	
	changeCategory('all');
});
function changeCategory(type){
$('.product_table').html('Please wait loading....');	
	if(type=='all'){
		var cid='all';
		var inid='all';	
		var catid='all';
		var user_id=$("#users").val();
		$.ajax({
			type:	'POST',
			dataType:'json',
			url:   	BASE_PATH+'/products/admin-product-ajax',
			data:	{user_id:user_id,country_id:cid,industry_id:inid,category_id:catid},
			success: function(data){
			$('.product_table').html('<table border="1" class="table table-bordered" cellpadding="0" cellspacing="0" width="100%" id="search_results"><thead><tr class="bg_header"><td>SNO</td><td>Generic Name</td><td width="77px;">Manufacturer&nbsp;</td><td>Quantity</td><td>Unites</td><td>Status</td><td>Action</td></tr></thead><tbody></tbody></table>');	
				if(data!='1'){
					eval(data);
					var oTable = $('#search_results').dataTable( {
							aoColumnDefs: [{bSortable: false,
											aTargets: [ 0 ]
											}],
							"bStateSave": true,
							"bAutoWidth": true,
							"sRowSelect": "multi",
							"sPaginationType": "full_numbers",
							"bProcessing": true,
							"aaData": data,
							"aoColumns": [
								{ "mData": "sno" },
								{ "mData": "genericname" },
								{ "mData": "manufacturer" },
								{ "mData": "quntity" },
								{ "mData": "unites" },
								{ "mData": "status" },
								{ "mData": "action" }
							] 
						});			
				}else{
					$('.product_table').html('No Products...');	
				}
			}
		});
	}else if(type=='country'){
		var cid=$("#country").val();
		var inid='all';	
		var user_id=$("#users").val();
		$.ajax({
			type:	'POST',
			url:   	BASE_PATH+'/products/get-industries',
			data:	{user_id:user_id,country_id:cid,industry_id:inid,check:'check'},
			success: function(data){
				if(data.output=='success'){				
					$("#industry").html(data.industries);
					if(cid==''){
						inid='all';
						changeIndustry(inid);
					}else{
						changeIndustry(inid);
					}
					var catid='all';
						$.ajax({
						type:	'POST',
						dataType:'json',
						url:   	BASE_PATH+'/products/admin-product-ajax',
						data:	{user_id:user_id,country_id:cid,industry_id:inid,category_id:catid},
						success: function(data){
						$('.product_table').html('<table border="1" cellpadding="0" cellspacing="0" width="100%" id="search_results"><thead><tr class="bg_header"><td>SNO</td><td>Generic Name</td><td width="77px;">Manufacturer&nbsp;</td><td>Quantity</td><td>Unites</td><td>Status</td><td>Action</td></tr></thead><tbody></tbody></table>');	
							if(data!='1'){
								eval(data);
								var oTable = $('#search_results').dataTable( {
										aoColumnDefs: [{bSortable: false,
														aTargets: [ 0 ]
														}],
										"bStateSave": true,
										"bAutoWidth": true,
										"sRowSelect": "multi",
										"sPaginationType": "full_numbers",
										"bProcessing": true,
										"aaData": data,
										"aoColumns": [
											{ "mData": "sno" },
											{ "mData": "genericname" },
											{ "mData": "manufacturer" },
											{ "mData": "quntity" },
											{ "mData": "unites" },
											{ "mData": "status" },
											{ "mData": "action" }
										] 
									});			
							}else{
								$('.product_table').html('No Products...');	
							}
						}
					});
				}
			}
		});
	}else if(type=='industry'){
		var cid=$("#country").val();
		if($("#industry").val()!=''){
			var inid=$("#industry").val();
		}else{
			var inid='all';
		}
		var user_id=$("#users").val();
		var check='check';		
		$.ajax({
			type:	'POST',
			url:   	BASE_PATH+'/products/get-categories',
			data:	{country_id:cid,industry_id:inid,check:'check'},
			success: function(data){
				if(data.output=='success'){
					$("#category").html(data.categories);
					if(inid=='all'){
						var catid='all';
					}else{
						if($("#category").val()!=''){
							var catid=$("#category").val();
						}else{
							var catid='all';
						}
					}
					$.ajax({
						type:	'POST',
						dataType:'json',
						url:   	BASE_PATH+'/products/admin-product-ajax',
						data:	{user_id:user_id,country_id:cid,industry_id:inid,category_id:catid},
						success: function(data){
						$('.product_table').html('<table border="1" cellpadding="0" cellspacing="0" width="100%" id="search_results"><thead><tr class="bg_header"><td>SNO</td><td>Generic Name</td><td width="77px;">Manufacturer&nbsp;</td><td>Quantity</td><td>Unites</td><td>Status</td><td>Action</td></tr></thead><tbody></tbody></table>');	
							if(data!='1'){
								eval(data);
								var oTable = $('#search_results').dataTable( {
										aoColumnDefs: [{bSortable: false,
														aTargets: [ 0 ]
														}],
										"bStateSave": true,
										"bAutoWidth": true,
										"sRowSelect": "multi",
										"sPaginationType": "full_numbers",
										"bProcessing": true,
										"aaData": data,
										"aoColumns": [
											{ "mData": "sno" },
											{ "mData": "genericname" },
											{ "mData": "manufacturer" },
											{ "mData": "quntity" },
											{ "mData": "unites" },
											{ "mData": "status" },
											{ "mData": "action" }
										] 
									});			
							}else{
								$('.product_table').html('No Products...');	
							}
						}
					});
				}
			}
		});
	}else if(type=='category'){
		var cid=$("#country").val();
		var inid=$("#industry").val();
		var catid=$("#category").val();
		var user_id=$("#users").val();
			$.ajax({
			type:	'POST',
			dataType:'json',
			url:   	BASE_PATH+'/products/admin-product-ajax',
			data:	{user_id:user_id,country_id:cid,industry_id:inid,category_id:catid},
			success: function(data){
			$('.product_table').html('<table border="1" cellpadding="0" cellspacing="0" width="100%" id="search_results"><thead><tr class="bg_header"><td>SNO</td><td>Generic Name</td><td width="77px;">Manufacturer&nbsp;</td><td>Quantity</td><td>Unites</td><td>Status</td><td>Action</td></tr></thead><tbody></tbody></table>');	
				if(data!='1'){
					eval(data);
					var oTable = $('#search_results').dataTable( {
							aoColumnDefs: [{bSortable: false,
											aTargets: [ 0 ]
											}],
							"bStateSave": true,
							"bAutoWidth": true,
							"sRowSelect": "multi",
							"sPaginationType": "full_numbers",
							"bProcessing": true,
							"aaData": data,
							"aoColumns": [
								{ "mData": "sno" },
								{ "mData": "genericname" },
								{ "mData": "manufacturer" },
								{ "mData": "quntity" },
								{ "mData": "unites" },
								{ "mData": "status" },
								{ "mData": "action" }
							] 
						});			
				}else{
					$('.product_table').html('No Products...');	
				}
			}
		});				
	}
}
function deleteProduct(pro_id){
	if(confirm("Are you sure you want delete record?")){
		window.location=BASE_URL+"/products/delete-products?product_id="+cat_id;					
	}
}
</script>
